import axios from 'axios'
import cookieUtils from '@/utils/cookieUtils'
// 创建axios实例
const service = axios.create({
  baseURL: 'http://localhost:8443', // api的base_url
  //timeout: 150000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    // 在发送请求之前做一些处理
    var userInfo = cookieUtils.getUserInfo();
    if (userInfo) {
        config.headers['headerUserId'] = userInfo.id;
    }

    var userToken = cookieUtils.getToken();
    if (userToken) {
        config.headers['headerUserToken'] = userToken;
    }
    return config
  },
  error => {
    // 处理请求错误
    console.log(error)
    return Promise.reject(error)
  }
)

// response拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log('err' + error)
    return Promise.reject(error)
  }
)

export default service